import { createApp } from 'vue'
import './assets/styles/index.less'
import 'vant/lib/index.css'
import App from './App.vue'
import VConsole from 'vconsole'
import { showSuccessToast, showFailToast, showToast, showDialog } from 'vant'
import { userApi } from '@/api'
import router from './router'
import wx from 'weixin-js-sdk'

// 设置根元素字体大小
const setRem = () => {
  const htmlWidth = document.documentElement.clientWidth || document.body.clientWidth
  const htmlDom = document.getElementsByTagName('html')[0]
  htmlDom.style.fontSize = htmlWidth / 10 + 'px'
}
window.onresize = setRem
setRem()

const getUrlParam = (name) => {
  const url = window.location.href;
  return new URL(url).searchParams.get(name);
}

const code = getUrlParam('code');
if (code) {
  userApi.getUserInfo(code).then(res => {
    const uid = res.data.uid || res.uid
    localStorage.setItem('token', res.data.userToken || res.userToken);
    localStorage.setItem('uid', uid )
    localStorage.setItem('userInfo', JSON.stringify(res.data));
    // localStorage.setItem('uid', '0c72610b7ae8b3ba5b4c9a5348b436c1')
    userApi.checkStatus(uid).then(status => {
      localStorage.setItem('status', status.data)
      if (status.data === 'false') {
        showDialog({
          message: '您还不是会员，请前往【首页-我要入会】加入工会，点击确认立即跳转',
          confirmButtonText: '确定',
        }).then(() => {
          wx.miniProgram.navigateTo({
            url: `/pages/index/index`
          })
        });
      }
      localStorage.setItem('type', status.type)
      const app = createApp(App)
      app.use(router)
      app.mount('#app')
    }).catch(err => {
      showToast(err)
    })
  }).catch(err => {
    showToast(err)
  })
} else {
  const token = localStorage.getItem('token')
  if (token) {
      const app = createApp(App)
      app.use(router)
      app.mount('#app')
  } else {
    showToast('登录信息无效')
  }
}